<?php
include '../includes/cPlayer.php';
include '../includes/cDatabase.php';
include '../includes/variables.php';
include '../includes/cPage.php';
include '../includes/cDates.php';

$cPage = new page("Create Database");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

if(@$_GET['form'] == "yes")
{
	$error = 0;
	
	// Firstly let's check that we've got our ffl_config database
	$cConfigDatabase = new database;
	
	$connect = $cConfigDatabase->connectToDatabase();
	$db = mysqli_select_db($connect, ffl_config);
	
	if ($db == 0)
	{
		// The ffl_config database does not exist, we should create one first
		$query = "CREATE DATABASE ffl_config";
		if (mysqli_query($connect, $query))
		{
			echo "<BR> Created ffl_config database";
			
			$db = mysqli_select_db($connect, ffl_config);
			
			$query = "CREATE TABLE configList (loginName	VARCHAR(8)	NOT NULL,
				password	VARCHAR(10)	NOT NULL,
				activeDatabase	VARCHAR(20)	PRIMARY KEY)";
			if (mysqli_query($connect, $query))
			{
				echo "<BR> Created table configlist within ffl_config database";
				$query = "INSERT INTO configlist (loginName, password, activeDatabase) 
				VALUES ('".$cConfigDatabase->user."', '".$cConfigDatabase->password."', 'ffl')";
				if (mysqli_query($connect, $query))
				{
					echo "<BR> Inserted data into table configlist";
				}
			}
		}
	}
	
	if ($teamOfTheMonth == "On")
		$teamOfTheMonth = 1;
	else
		$teamOfTheMonth = 0;
		
	if ($captains == "On")
		$captains = 1;
	else
		$captains = 0;
		
	if ($userlogins == "On")
		$userlogins = 1;
	else
		$userlogins = 0;
	
	$cDatabase = new database;
		
	$database = $cDatabase->createDatabase(	$_POST['startYear'], 
											$_POST['maxTransfers'], 
											$_POST['maxMembers'], 
											$_POST['maxWeeks'], 
											$_POST['maxTeamValue']
											);

	if ($database)
	{
		// Add config to config TABLE

		$connect = $cDatabase->connectToDatabase();
		$db = $cDatabase->selectDatabase($connect, $database);
		
		$playersPerTeam = PLAYERSPERTEAM;
		
		$error = $cDatabase->insertConfig($connect, $startYear, $startMonth, $startDay, $maxTeamValue, $maxTransfers, 
			$playersPerTeam, $maxWeeks, $entryFee, $transferFee, $numberPrizes, $teamOfTheMonth, $captains, $userlogins);
	}
	else
	{
		echo "<BR>ERROR: Database not created";
	}
}

?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>?form=yes" method="POST">
  <table width='400' border='0'>
    <tr><td></td>
		<td>Day</td>
		<td>Month</td>
		<td>Year</td>
    </tr>
    <tr>
	<td>First Game Of The Season: Saturday</td>
 		<td><input type='text' name='startDay' size='20' maxlength='2' value=<?php echo @$_POST['startDay'] ?>></td>
		<td><input type='text' name='startMonth' size='20' maxlength='2' value=<?php echo @$_POST['startMonth'] ?>></td>
		<td><input type='text' name='startYear' size='20' maxlength='4' value=<?php echo @$_POST['startYear'] ?>></td>
	</tr>  
	<tr><td><BR></td></tr>
	<tr><td>Max Transfers</td>
		<td>Max Team Value</td>
		<td>Max Weeks</td>
	</tr>
	<tr>
		<td><input type='text' name='maxTransfers' size='20' maxlength='2' value=<?php echo @$_POST['maxTransfers'] ?>></td>
		<td><input type='text' name='maxTeamValue' size='20' maxlength='3' value=<?php echo @$_POST['maxTeamValue'] ?>></td>
		<td><input type='text' name='maxWeeks' size='20' maxlength='2' value=<?php echo @$_POST['maxWeeks'] ?>></td>
	</tr>
	<tr><td><BR></td></tr>
	<tr><td>Entry Fee</td>
		<td>Transfer Fee</td>
		<td>Number Of Prizes</td>
	</tr>
	<tr>
		<td><input type='text' name='entryFee' size='20' maxlength='2' value=<?php echo @$_POST['entryFee'] ?>></td>
		<td><input type='text' name='transferFee' size='20' maxlength='2' value=<?php echo @$_POST['transferFee'] ?>></td>
		<td><input type='text' name='numberPrizes' size='20' maxlength='1' value=<?php echo @$_POST['numberPrizes'] ?>></td>
	</tr>
	<tr><td><BR></td></tr>
	<tr>
		<td>Team Of The Month Feature</td>
		<td>Captains</td>
		<td>User Logins</td>
	</tr>
	<tr>
   		<td><select name="teamOfTheMonth"><option>On</option><option>Off</option></select></td>
		<td><select name="captains"><option>On</option><option>Off</option></select></td>
		<td><select name="userlogins"><option>On</option><option>Off</option></select></td>
	</tr>
	</table>
	<BR><BR><BR>
	<div align="center"><table>
	<tr>       
    <td><input name="createDatabaseButton" type="submit" id="createDatabase" value="Create Database">        
	</td>
	</table></div>
<?php
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>